/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddBillFrm.java
 *
 * Created on 2012-1-5, 15:16:02
 */
package billmanage;

import com.breeze.entity.Tbill;
import com.breeze.entity.Tcategory;
import com.breeze.entity.Tuser;
import com.breeze.entity.Tuserbill;
import com.breeze.service.ICoreService;
import com.breeze.util.Constants;
import com.breeze.util.SpringUtil;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import org.jdesktop.application.Action;

/**
 *
 * @author huateng
 */
public class AddBillFrm extends javax.swing.JFrame {

    /** Creates new form AddBillFrm */
    public AddBillFrm() {
        initComponents();
        coreService = (ICoreService) SpringUtil.getBean("coreService");
        initComboBox();
        
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e){
                AddBillFrm.this.dispose();
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        tf_billNo = new javax.swing.JTextField();
        tf_price = new javax.swing.JTextField();
        cb_category = new javax.swing.JComboBox();
        cb_payMan = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        tf_response = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        tf_remark = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(billmanage.BillManageApp.class).getContext().getResourceMap(AddBillFrm.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        tf_billNo.setText(resourceMap.getString("tf_billNo.text")); // NOI18N
        tf_billNo.setName("tf_billNo"); // NOI18N

        tf_price.setText(resourceMap.getString("tf_price.text")); // NOI18N
        tf_price.setName("tf_price"); // NOI18N

        cb_category.setName("cb_category"); // NOI18N

        cb_payMan.setName("cb_payMan"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        tf_response.setText(resourceMap.getString("tf_response.text")); // NOI18N
        tf_response.setName("tf_response"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(billmanage.BillManageApp.class).getContext().getActionMap(AddBillFrm.class, this);
        jButton1.setAction(actionMap.get("selectResponseMen")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jButton2.setAction(actionMap.get("Post")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        tf_remark.setText(resourceMap.getString("tf_remark.text")); // NOI18N
        tf_remark.setName("tf_remark"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(51, 51, 51)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton2)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(tf_remark, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tf_billNo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                            .addComponent(tf_price, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cb_category, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(cb_payMan, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(tf_response, javax.swing.GroupLayout.Alignment.LEADING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)))
                .addContainerGap(32, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tf_billNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tf_price, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cb_category, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(cb_payMan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(tf_response, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(tf_remark, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addGap(32, 32, 32))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(AddBillFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(AddBillFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(AddBillFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(AddBillFrm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AddBillFrm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cb_category;
    private javax.swing.JComboBox cb_payMan;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField tf_billNo;
    private javax.swing.JTextField tf_price;
    private javax.swing.JTextField tf_remark;
    private javax.swing.JTextField tf_response;
    // End of variables declaration//GEN-END:variables
    private ICoreService coreService;
    private Map categoryMap=new HashMap();
    private Map paymanMap=new HashMap();
//    private Tcategory selectedCategory;
//    private Tuser selectedUser;
    private Set responseSet=null;
    
    private void initComboBox() {
        List userList=coreService.queryUserList(null);
        Object[] items=new Object[userList.size()];
        int index=0;
        for(Object o:userList){
            Tuser u=(Tuser)o;
            paymanMap.put(u.getUsername(), u);
            items[index++]=u.getUsername();
        }
        ComboBoxModel model=new DefaultComboBoxModel(items);
        cb_payMan.setModel(model);
        
        List cateList=coreService.queryCategoryList(null);
        Object[] cateItems=new Object[cateList.size()];
        index=0;
        for(Object o:cateList){
            Tcategory c=(Tcategory)o;
            categoryMap.put(c.getDisplay(), c);
            cateItems[index++]=c.getDisplay();
        }
        ComboBoxModel model1=new DefaultComboBoxModel(cateItems);
        cb_category.setModel(model1);
    }
    
    public void setResponseSet(Set set){
        this.responseSet=set;
    }

    @Action
    public void selectResponseMen() {
        AddResponseDlg dlg=new AddResponseDlg(this, true);
        dlg.setVisible(true);
        showResponseMen();
    }

    private void showResponseMen() {
        if(responseSet!=null){
            StringBuilder sb=new StringBuilder();
            for(Object o:responseSet){
                Tuser u=(Tuser)o;
                sb.append(u.getUsername()).append(";");
            }
            tf_response.setText(sb.toString());
        }
    }

    @Action
    public void Post() {
        //add bill
        Tbill bill=new Tbill();
        bill.setBillno(tf_billNo.getText());
        bill.setFee(tf_price.getText());
        Tcategory selectedCate=(Tcategory)categoryMap.get(cb_category.getSelectedItem());
        bill.setCategoryid(selectedCate.getSeqid());
        Tuser selectedUser=(Tuser)paymanMap.get(cb_payMan.getSelectedItem());
        bill.setPaymanid(selectedUser.getSeqid());
        bill.setState("0");//初始状态
        bill.setRemark(tf_remark.getText());
        bill.setCreateDt(Constants.DATE_FORMAT.format(new Date()));
        int billId=(Integer)coreService.insertBill(bill);
        
        //add userbill
        for(Object o:responseSet){
                Tuser u=(Tuser)o;
                Tuserbill userbill=new Tuserbill();
                userbill.setBillid(billId);
                userbill.setUserid(u.getSeqid());
                coreService.insertUserbill(userbill);
        }
        
        JOptionPane.showMessageDialog(this, "add bill successful");
        this.dispose();
    }
}
